#!/bin/bash

# 安装 pgsql
function setup_pgsql()
{
  echo_start "$1 ... ..."
  confirm_operate "install dbserver"
  if [ $? -eq 0 ]; then
    # 停止pgsql
    if [ -d "$HRS_PGSQL_HOME/bin" ] && [ -f "$HRS_PGSQL_HOME/bin/pg_ctl" ]; then
      local PGSQLPID=$(ps ax | grep postgres | grep "$HRS_PGSQL_PGDATA" | grep -v grep | awk '{print $1}')
      echo_trace "postgres PGSQLPID=$PGSQLPID"
      if [ -n "$PGSQLPID" ]; then
        echo ""
        echo_info "stop dbserver ... ..."
        su - $HYREN_USER -c "$HRS_PGSQL_HOME/bin/pg_ctl -D $HRS_PGSQL_PGDATA -m immediate stop"
        waiting_proc_status $HRS_PGSQL_PORT 1 "db stop"
      fi
    else
      echo_info "dbserver not exist in $HRS_PGSQL_HOME, starting install now."
    fi
    
    install_flag_file="/tmp/hrs_install_flag_pgsql_makeinstall.pass"
    #echo_trace "check latest install flag file : `ls $install_flag_file`"
    build_pgsql="true"
    # 上次已经安装成功，则询问是否重新编译安装
    if [ -f $install_flag_file ]; then
      echo
      echo "    dbserver has been build on the last time."
      confirm_operate "rebuild dbserver"
      if [ $? -ne 0 ]; then
        build_pgsql="false"
      else
        rm -f $install_flag_file
      fi
    fi
    echo_trace "build_pgsql=$build_pgsql"
    if [ "$build_pgsql" = "true" ]; then
      yum_install readline-devel
      yum_install zlib-devel
      yum_install flex
      yum_install bison

      # 清理上次安装的文件
      # make uninstall // 卸载已安装的文件，不会删除掉任何创建出来的目录
      # make clean  //清理编译过程中生成的文件，并释放磁盘空间
      # make distclean  // 将源码树恢复成发布的状态
      cp  $XDIR/packages/postgresql-9.6.5.tar.gz  /tmp/
      rm -rf /tmp/postgresql-9.6.5
      tar -zxf /tmp/postgresql-9.6.5.tar.gz -C /tmp/
      cd /tmp/postgresql-9.6.5

      if [ -d $HRS_PGSQL_PGDATA ]; then rm -rf $HRS_PGSQL_PGDATA; fi
      if [ -d $HRS_PGSQL_LOGS ]; then rm -rf $HRS_PGSQL_LOGS; fi
      if [ -d $HRS_PGSQL_HOME ]; then rm -rf $HRS_PGSQL_HOME; fi
      echo_info "init env done."

      echo_doing "configure, please waiting ... ..."
      ./configure --prefix=$HRS_PGSQL_HOME > $SETUP_LOGDIR/dbserver_configure.log
      if [ $? -ne 0 ]; then
        echo
        die "configure fail, installing abort! Check infomation in config.log or $SETUP_LOGDIR/dbserver_configure.log"
      fi
      echo_done

      echo_doing "make, please waiting ... ..."
      make -j > $SETUP_LOGDIR/dbserver_make.log
      if [ $? -ne 0 ]; then
        echo
        die "make fail, installing abort! Check infomation in $SETUP_LOGDIR/dbserver_make.log"
      fi
      echo_done

      echo_doing "install, please waiting ... ..."
      make install > $SETUP_LOGDIR/dbserver_install.log
      if [ $? -ne 0 ]; then
        echo
        die "install fail, installing abort! Check infomation in $SETUP_LOGDIR/dbserver_install.log"
      fi
      echo_done

      #安装file_fdw
      cd /tmp/postgresql-9.6.5/contrib/file_fdw
      echo_doing "make_file_fdw, please waiting ... ..."
      make -j > $SETUP_LOGDIR/file_fdw_make.log
      if [ $? -ne 0 ]; then
        echo
        die "make_file_fdw fail, installing abort! Check infomation in $SETUP_LOGDIR/file_fdw_make.log"
      fi
      echo_done

      echo_doing "install file_fdw, please waiting ... ..."
      make install > $SETUP_LOGDIR/file_fdw_install.log
      if [ $? -ne 0 ]; then
        echo
        die "install fail, installing abort! Check infomation in $SETUP_LOGDIR/dbserver_install.log"
      fi
      echo_done

      mkdir_check $HRS_PGSQL_LOGS
      mkdir_check $HRS_PGSQL_PGDATA

      #chmod +x  $HRS_PGSQL_HOME/bin/*
      chown -R $HYREN_USER:$HYREN_USER $HRS_PGSQL_HOME
      chown -R $HYREN_USER:$HYREN_USER $HRS_PGSQL_PGDATA
      chown -R $HYREN_USER:$HYREN_USER $HRS_PGSQL_LOGS
      
      #touch $install_flag_file 有BUG，不能这么用
    fi

    su - $HYREN_USER -s /bin/bash $XDIR/util/pgsql_suser.sh $build_pgsql
    die_insh_check

    # 这里配置开机启动：参考源码文件：contrib/start-scripts/linux

    # 因为是su成了另外用户执行脚本，该脚本没执行完就会走到下面，所以睡眠10秒等结束
    # sleep 10
    waiting_proc_status $HRS_PGSQL_PORT 0 "db start"
    sleep 1

    # 设置环境变量
    #set_bash_profile "HRS PGSQL ENV" "HRSCOMMENT" "$HYREN_USER"
    set_bash_profile "HRS_PGSQL_HOME" "$HRS_PGSQL_HOME" "$HYREN_USER"
    set_bash_profile "HRS_PGSQL_LOGS" "$HRS_PGSQL_LOGS" "$HYREN_USER"
    set_bash_profile "HRS_PGSQL_PGDATA" "$HRS_PGSQL_PGDATA" "$HYREN_USER"
    set_bash_profile "HRS_PGSQL_PORT" "$HRS_PGSQL_PORT" "$HYREN_USER"

    echo_success "$1"
    echo_info "****************************************"
    echo_info "DBRUN HOME    dir : $HRS_PGSQL_HOME"
    echo_info "DBRUN LOGS    dir : $HRS_PGSQL_LOGS"
    echo_info "DBRUN DATA    dir : $HRS_PGSQL_PGDATA"
    echo_info "DBRUN SERVER PORT : $HRS_PGSQL_PORT"
    echo_info "****************************************"

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}
